package com.forward.utils;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
/**
 * @Author: shafei
 * @Date: 2025/02/05 19:28
 * @description：
 **/
public class HttpUtil {
    private static final HttpClient client = HttpClient.newHttpClient();

    public static String sendGetRequest(String url, String token) throws Exception {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("x-access-token", token)
                .GET()
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
        return response.body();
    }

    public static String sendPostRequest(String url, String jsonBody, String token) throws Exception {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/json")
                .header("x-access-token",   token)
                .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
        return response.body();
    }

    public static String sendPutRequest(String url, String jsonBody, String token) throws Exception {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("Content-Type", "application/json")
                .header("x-access-token",  token)
                .PUT(HttpRequest.BodyPublishers.ofString(jsonBody))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
        return response.body();
    }

    public static String sendDeleteRequest(String url, String token) throws Exception {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .header("x-access-token",   token)
                .DELETE()
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
        return response.body();
    }

    public static void main(String[] args) {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MzkwNTMxNjEsInVzZXJuYW1lIjoiYWRtaW4ifQ.-AOfKFxyrxrSPGGLvIK4FH-dQPIQtHZdn8tpgelz9DI"; // 替换为你的实际 token

        String url = "http://localhost:8081";
//        String url = "http://doll.org.cn:8081";
        try {

            String loginBody = "{\"mobile\":\"19905818027\",\"captcha\":\"123456\"}";

            String loginResponse = sendPostRequest(url+"/sys/phoneLogin", loginBody, token);
            System.out.println("GET Response: " + loginResponse);
//            // 示例 GET 请求
            String getListResponse = sendGetRequest(url+"/doll/products/list?pageNo=1&pageSize=10", token);
            System.out.println("GET Response: " + getListResponse);
////
//            // 示例 POST 请求
//            String addProductBody = "{\"productName\":\"New Product\", \"productDescription\":\"Description of new product\"}";
//            String addProductResponse = sendPostRequest("http://localhost:8081/doll/products/add", addProductBody, token);
//            System.out.println("POST Response: " + addProductResponse);
//
//            // 示例 PUT 请求
//            String editProductBody = "{\"productId\":\"1\", \"productName\":\"Updated Product\", \"productDescription\":\"Updated description\"}";
//            String editProductResponse = sendPutRequest("http://localhost:8081/doll/products/edit", editProductBody, token);
//            System.out.println("PUT Response: " + editProductResponse);
//
//            // 示例 DELETE 请求
//            String deleteProductResponse = sendDeleteRequest("http://localhost:8081/doll/products/remove?id=1", token);
//            System.out.println("DELETE Response: " + deleteProductResponse);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
